package com.wskj.workbench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.workbench.bo.web.TbUserReceiveOrderAddBo;
import com.wskj.workbench.bo.web.TbUserReceiveOrderEditBo;
import com.wskj.workbench.bo.web.TbUserReceiveOrderQueryBo;
import com.wskj.workbench.vo.web.TbUserReceiveOrderVo;
import org.springframework.stereotype.Service;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.workbench.domain.TbUserReceiveOrder;
import com.wskj.workbench.mapper.TbUserReceiveOrderMapper;
import com.wskj.workbench.service.ITbUserReceiveOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户订单配送地址Service业务层处理
 *
 * @author wsdz
 * @date 2023-03-07
 */
@Service
public class TbUserReceiveOrderServiceImpl extends ServicePlusImpl<TbUserReceiveOrderMapper, TbUserReceiveOrder> implements ITbUserReceiveOrderService {

    @Override
    public TbUserReceiveOrderVo queryById(Long receiveOrderId){
        return getVoById(receiveOrderId, TbUserReceiveOrderVo.class);
    }

    @Override
    public TableDataInfo<TbUserReceiveOrderVo> queryPageList(TbUserReceiveOrderQueryBo bo) {
        PagePlus<TbUserReceiveOrder, TbUserReceiveOrderVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TbUserReceiveOrderVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<TbUserReceiveOrderVo> queryList(TbUserReceiveOrderQueryBo bo) {
        return listVo(buildQueryWrapper(bo), TbUserReceiveOrderVo.class);
    }

    private LambdaQueryWrapper<TbUserReceiveOrder> buildQueryWrapper(TbUserReceiveOrderQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbUserReceiveOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getReceiveId() != null, TbUserReceiveOrder::getReceiveId, bo.getReceiveId());
        lqw.eq(StrUtil.isNotBlank(bo.getUserId()), TbUserReceiveOrder::getUserId, bo.getUserId());
        lqw.eq(bo.getSex() != null, TbUserReceiveOrder::getSex, bo.getSex());
        lqw.like(StrUtil.isNotBlank(bo.getReceiveName()), TbUserReceiveOrder::getReceiveName, bo.getReceiveName());
        lqw.eq(StrUtil.isNotBlank(bo.getReceivePhone()), TbUserReceiveOrder::getReceivePhone, bo.getReceivePhone());
        lqw.eq(bo.getProvinceId() != null, TbUserReceiveOrder::getProvinceId, bo.getProvinceId());
        lqw.eq(StrUtil.isNotBlank(bo.getProvince()), TbUserReceiveOrder::getProvince, bo.getProvince());
        lqw.eq(bo.getAreaId() != null, TbUserReceiveOrder::getAreaId, bo.getAreaId());
        lqw.eq(StrUtil.isNotBlank(bo.getArea()), TbUserReceiveOrder::getArea, bo.getArea());
        lqw.eq(bo.getCityId() != null, TbUserReceiveOrder::getCityId, bo.getCityId());
        lqw.eq(StrUtil.isNotBlank(bo.getCity()), TbUserReceiveOrder::getCity, bo.getCity());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), TbUserReceiveOrder::getAddress, bo.getAddress());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(TbUserReceiveOrderAddBo bo) {
        TbUserReceiveOrder add = BeanUtil.toBean(bo, TbUserReceiveOrder.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(TbUserReceiveOrderEditBo bo) {
        TbUserReceiveOrder update = BeanUtil.toBean(bo, TbUserReceiveOrder.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TbUserReceiveOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importTzUserReceiveOrder(List<TbUserReceiveOrderVo> tzUserReceiveOrderList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(tzUserReceiveOrderList), "导入数据不能为空");
        int successNum = 0;
        for (TbUserReceiveOrderVo tzUserReceiveOrder : tzUserReceiveOrderList) {
            TbUserReceiveOrder add = BeanUtil.toBean(tzUserReceiveOrder, TbUserReceiveOrder.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", tzUserReceiveOrderList.size(), successNum);
        if (successNum != tzUserReceiveOrderList.size()) {
            msg = msg + String.format("失败 %d 条！", tzUserReceiveOrderList.size() - successNum);
        }
        return msg;
    }
}
